package cloudjunit;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import Common.TraceWriter;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.autoscaling.AmazonAutoScaling;
import com.amazonaws.services.autoscaling.AmazonAutoScalingClient;
import com.amazonaws.services.cloudfront.model.InvalidArgumentException;
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.dynamodb.AmazonDynamoDBClient;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflow;
import com.amazonaws.services.simpleworkflow.AmazonSimpleWorkflowClient;

import org.apache.log4j.BasicConfigurator;

public class Config 
{
	public static ConfigHelper data = null;
	
	public static void initConfigWithConfigFile()
	{
		data = ConfigHelper.createConfig();
	}
	
	public static void initConfigWithDefaults(String accessId, String secretKey, String defaultS3Bucket, String domain)
	{
		data = ConfigHelper.createConfig(accessId, secretKey, defaultS3Bucket, domain);
	}
	
	//////////////////////////////////////////////
    
    public static class ConfigHelper
    {
    	private Properties sampleConfig;
    	
    	public final String accessId;
    	public final String secretKey;
    	public final String defaultS3Bucket;
    	public final String domain;

    	private ConfigHelper(String accessId, String secretKey,	String defaultS3Bucket, String domain)
    	{
    		this.accessId = accessId;
        	this.secretKey = secretKey;
        	this.defaultS3Bucket = defaultS3Bucket;
        	this.domain = domain;
    	}
    	
    	private ConfigHelper(File propertiesFile) throws IOException
    	{
    		FileInputStream inputStream = new FileInputStream(propertiesFile);
    		sampleConfig = new Properties();
    		sampleConfig.load(inputStream);

    		accessId = sampleConfig.getProperty("AWS.Access.Key");
    		checkNotEmpty(accessId);
    		secretKey = sampleConfig.getProperty("AWS.Secret.Key");
    		checkNotEmpty(secretKey);

    		defaultS3Bucket = sampleConfig.getProperty("S3.DefaultBucketName");
    		checkNotEmpty(defaultS3Bucket);

    		domain = sampleConfig.getProperty("SWF.DomainName");
    		checkNotEmpty(domain);
    	}
    	
    	private void checkNotEmpty(String str)
    	{
    		if (str.isEmpty())
    			throw new IllegalArgumentException();
    	}
    	
    	public static ConfigHelper createConfig()
    	{
    		try
    		{
	    		BasicConfigurator.configure();
	    		org.apache.log4j.Logger.shutdown();
	
	    		File accessProperties = new File(ConfigParams.CONFIG_FILE_PATH);
	
	    		if (!accessProperties.exists())
	    			throw new FileNotFoundException("Cannot find config file: " + ConfigParams.CONFIG_FILE_PATH);
	
	    		return new ConfigHelper(accessProperties);
    		}
	    	catch(Exception e)
	    	{
	    		System.err.println("createConfig() FAILED!!!");
	    		e.printStackTrace();
	    		throw new RuntimeException();
	    	}
    	}
    	
    	public static ConfigHelper createConfig(String accessId, String secretKey,	String defaultS3Bucket, String domain)
    	{
    		return new ConfigHelper(accessId, secretKey, defaultS3Bucket, domain);
    	}
    }
    
    ////////////////////////////////////
	
	static public AmazonSimpleWorkflow createSWFClient()
	{
        ClientConfiguration config = new ClientConfiguration().withSocketTimeout(70 * 1000);
        AWSCredentials awsCredentials = new BasicAWSCredentials(data.accessId, data.secretKey);
        AmazonSimpleWorkflow client = new AmazonSimpleWorkflowClient(awsCredentials, config);
        client.setEndpoint(ConfigParams.SWF_SERVICE_URL);
        return client;
    }

    static public AmazonS3 createS3Client()
    {
        AWSCredentials s3AWSCredentials = new BasicAWSCredentials(data.accessId, data.secretKey);
        AmazonS3 client = new AmazonS3Client(s3AWSCredentials);
        return client;
    }
    
    static public AmazonAutoScaling createAutoScaleClient()
    {
        AWSCredentials awsCredentials = new BasicAWSCredentials(data.accessId, data.secretKey);
        AmazonAutoScaling client = new AmazonAutoScalingClient(awsCredentials);
        return client;
    }
    
    static public AmazonCloudWatch createCloudWatchClient()
    {
        AWSCredentials awsCredentials = new BasicAWSCredentials(data.accessId, data.secretKey);
        AmazonCloudWatch client = new AmazonCloudWatchClient(awsCredentials);
        return client;
    }
    
    static public AmazonDynamoDBClient createDynamoDBClient()
    {
        AWSCredentials s3AWSCredentials = new BasicAWSCredentials(data.accessId, data.secretKey);
        AmazonDynamoDBClient client = new AmazonDynamoDBClient(s3AWSCredentials);
        return client;
    }
}